package cn.endeavour.pet.controller;

import cn.endeavour.basic.exception.BusinessException;
import cn.endeavour.basic.util.AjaxResult;
import cn.endeavour.basic.util.PageList;
import cn.endeavour.pet.domain.Pet;
import cn.endeavour.pet.domain.PetDetail;
import cn.endeavour.pet.query.PetQuery;
import cn.endeavour.pet.service.IPetDetailService;
import cn.endeavour.pet.service.IPetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
 * 宠物接口
 */
@Api(tags = "宠物接口",description = "宠物接口详细描述")
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @Autowired
    private IPetDetailService detailService;

    /**
     * 判断是否为管理员还是店铺管理员
     * @param request 请求
     * @return 处理结果
     */
    @GetMapping("/isAdmin")
    public AjaxResult isAdmin(HttpServletRequest request) {
        try {
            petService.isAdmin(request);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统错误，"+e.getMessage()).setSuccess(false);
        }
    }

    /**
     * 领养宠物
     * @param id 宠物id
     * @return 处理结果
     */
    @GetMapping("/adopt/{id}")
    public AjaxResult adopt(@PathVariable("id") Long id, HttpServletRequest request) {
        try {
            petService.adopt(id, request);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("领养失败，"+e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统错误，"+e.getMessage()).setSuccess(false);
        }
    }

    /**
     * 宠物上架
     * @param ids 需要上架宠物的id
     * @return 处理的结果
     */
    @PostMapping("/onSale")
    @ApiOperation(value = "宠物上架接口",notes = "传入批量需要上架的宠物的id组成的数组")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败，"+e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统错误，"+e.getMessage()).setSuccess(false);
        }
    }

    /**
     * 宠物下架
     * @param ids 需要下架的宠物
     * @return 处理结果
     */
    @PostMapping("/offSale")
    @ApiOperation(value = "宠物下架接口",notes = "传入批量需要下架的宠物的id组成的数组")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败，"+e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统错误，"+e.getMessage()).setSuccess(false);
        }
    }

    /**
     * 批量删除 //如果前台传递的是数组，后台可以使用功能list或数组接收
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids) {
        try {
            petService.patchDelete(ids);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败，"+e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统错误，"+e.getMessage()).setSuccess(false);
        }
    }

    /**
     * 查询宠物细节接口
     * @param id 宠物id
     * @return 宠物细节
     */
    @GetMapping("/detail/{id}")
    public PetDetail findDetail(@PathVariable("id") Long id) {
        return detailService.findDetailByPetId(id);
    }

    /**
     * 查询单个宠物
     * @param id 查询的id
     * @return 查询到的对象
     */
    @ApiOperation(value = "查询单个宠物",notes = "需要传递id")
    @GetMapping("/{id}")
    public Pet findById(@PathVariable("id")Long id){
        return petService.findById(id);
    }

    /**
     * 查询所有
     * @return 所有的宠物
     */
    @ApiOperation(value = "查询所有",notes = "不需参数")
    @GetMapping
    public List<Pet> findAll(){
        return petService.findAll();
    }

    /**
     * 分页查询宠物信息
     * @param petQuery 宠物分页查询信息
     * @return 当前页的宠物信息
     */
    @ApiOperation(value = "分页查询宠物信息",notes = "需要传递当前页和每页显示的条数")
    @PostMapping
    public PageList<Pet> findPage(@RequestBody PetQuery petQuery){
        return petService.queryPage(petQuery);
    }

    /**
     * 删除：delete
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "需要传递id")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            petService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败，"+e.getMessage()).setSuccess(false);
        }
    }

    /**
     * 添加和修改【差异在于是否有id】：put 和 post
     * @param pet
     * @return
     */
    @PutMapping
    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    //Put http://localhost:8080/pet + json参数pet
    public AjaxResult addOrUpdate(@RequestBody Pet pet){
        try {
            if(pet.getId() == null){
                petService.add(pet);
            }else{
                petService.update(pet);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败，"+e.getMessage()).setSuccess(false);
        }
    }

}
